#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/stat.h>
#include<time.h>
#include<pwd.h>
void mySort(char *str[100])
{
  int length=0;
  while(str[length++]);
  int i=0;
  int j=0;
  char *p;
  for(;i<length-1;i++)
  {
	  for(j=0;j<length-i-2;j++)
	  {
		  if(strcmp(str[j],str[j+1])>0)
	    {
	       p=str[j];
		   str[j]=str[j+1];
	       str[j+1]=p;
	    }
      }
  }


}
int  main(int argc,char *argv[])
{
	
	if(argc>1)
	{
	  if(strncmp(argv[1],"-",1)!=0)
	  {
	    printf("option error!\n");
		return;
	  
	  }
	
	}
	
	char path[100]={0};
	getcwd(path,100);
	DIR *dir=opendir(path);
    if(dir==NULL)
	{
	  printf("open dir fail!\n"); 
	  return 0;
	}
    
	struct dirent *p;
	char *str[100]={NULL};
	int i=0;

	while(p=readdir(dir))
     str[i++]=p->d_name;
		
	mySort(str);
	i=0;
    struct stat s;
	
   
		//no option
	if(argc==1)
	{
		  while (str[i])
		  {
			  lstat(str[i],&s);
			
		    if(strncmp(str[i],".",1)==0)
			{   i++;
				continue;
			}
	       
			if(S_ISDIR(s.st_mode))
			
				printf("\033[1;34m%s  \033[0m",str[i++]);
			    
			
			else if(S_ISREG(s.st_mode) && (s.st_mode & S_IXUSR||s.st_mode & S_IXGRP||s.st_mode &S_IXOTH))
			
			   printf("\033[1;32m%s  \033[0m",str[i++]);
			
			
			else 
			
				printf("%s  ",str[i++]);
	      }
	}
	
	//have option
	else if(argc>1)
	{
	   ///////////////////////////////////////////	   
		int i=1;
		int flaga=0;
		int flagl=0;
	  int total=4;
	 int blocksize=0;
	  int totalflag=1;
		while(i<argc)
		{
			
	    	if(strstr(argv[i],"a")!=0)
			flaga++;

			if(strstr(argv[i],"l")!=0)
			flagl++;
			
			i++;
		}
		//////////////////////////////////
		  i=0;
		  while(str[i])
		  {
			  lstat(str[i],&s);
		    //////////ls - ///no -a//skip a////
			  if(flaga==0)
			 {
			    if(strncmp(str[i],".",1)==0)
				{
					i++;
			     	continue;
				
				}
			 }
             ///////// ls -xlx//////////
		     if(flagl!=0)
			 {
                   ///////////jisuan total/////
				 if(totalflag==1)
				 {
				    int k=0;
					struct stat stmp;
					
				  while(str[k])
				  {
                          lstat(str[k],&stmp);
				    if(flaga==0 && strncmp(str[k],".",1)==0)
					{   
						k++;
						continue;
					}

					if((blocksize+stmp.st_size)>4096)
			        {
						if(stmp.st_size<=4096)
						{
						total+=4;
						blocksize=stmp.st_size;
						k++;
						}
				    	
						else if(stmp.st_size>4096)
						{
					    	int tmpsize=stmp.st_size;
						    while(tmpsize>4096)
					    	{
						     total+=4;
						     tmpsize-=4096;
						
					       	}
					     	total+=4;
						    blocksize=tmpsize;
						    k++;  
						}
					}
				
					else 
					{
				
						blocksize+=stmp.st_size;
						k++;
					
					}

				  }
				  printf("total %d\n",total);
				   totalflag=0;
				 }
				 //////////mod////////
				char mod[12]={0};
				strcpy(mod,"----------.");
				if(S_ISREG(s.st_mode))
					mod[0]='-';
				else if(S_ISDIR(s.st_mode))
					mod[0]='d';
				else if(S_ISLNK(s.st_mode))
					mod[0]='l';
				else if(S_ISFIFO(s.st_mode))
					mod[0]='p';
				else if(S_ISSOCK(s.st_mode))
					mod[0]='s';
				else if(S_ISBLK(s.st_mode))
					mod[0]='b';
				else if(S_ISCHR(s.st_mode))
					mod[0]='c';

               if(s.st_mode & S_IRUSR)
				   mod[1]='r';
			   if(s.st_mode & S_IWUSR)
				   mod[2]='w';
			   if(s.st_mode & S_IXUSR)
				   mod[3]='x';
			   if(s.st_mode & S_IRGRP)
				   mod[4]='r';
			   if(s.st_mode & S_IWGRP)
				   mod[5]='w';
			   if(s.st_mode & S_IXGRP)
				   mod[6]='x';
			   if(s.st_mode & S_IROTH)
				   mod[7]='r';
			   if(s.st_mode & S_IWOTH)
				   mod[8]='w';
			   if(s.st_mode & S_IXOTH)
				   mod[9]='x';

				 
				 ////////////////////
                
                /////ctime////////////
				struct tm *timedate;
				timedate=localtime(&s.st_ctime);
				char *tmpdate=asctime(timedate);
				char date[15]={0};

				int n=4;
				int m=0;
				for(;n<16;n++,m++)
					date[m]=tmpdate[n];
			
				////////owner group name/////////

				struct passwd *powner=NULL;
				struct passwd *pgrper=NULL;
				powner=getpwuid(s.st_uid);
				pgrper=getpwuid(s.st_gid);
		
			
			printf("%s %d %-4s %-4s %4d %s ",mod,s.st_nlink,powner->pw_name,pgrper->pw_name,s.st_size,date);
			  
		
			  if(S_ISDIR(s.st_mode))
			
			   {
				   printf("\033[1;34m%s  \033[0m\n",str[i++]);
			         continue;
			   
			   }
			    
			  else if(S_ISREG(s.st_mode) && (s.st_mode & S_IXUSR||s.st_mode & S_IXGRP||s.st_mode &S_IXOTH))
			
			  {
				  printf("\033[1;32m%s  \033[0m\n",str[i++]);
			         continue;
			  }
			 
			   printf("%s\n",str[i++]);
			  continue;
			 }
		  //////////////////////
              
			 
		////// ls -/////no -a no -l/////////	 
			if(S_ISDIR(s.st_mode))
			
				printf("\033[1;34m%s  \033[0m",str[i++]);
			    
			
			else if(S_ISREG(s.st_mode) && (s.st_mode & S_IXUSR||s.st_mode & S_IXGRP||s.st_mode &S_IXOTH))
			
			   printf("\033[1;32m%s  \033[0m",str[i++]);
			 
			else 
			printf("%s  ",str[i++]);
		  
		  }
		
		
	}
	
	
	printf("\n");
	return 0;
}
